package com.sheng.base;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;


public class Producer {
    public static void main(String[] args) throws Exception {
        //1.创建一个发送消息的对象Producer
        DefaultMQProducer producer = new DefaultMQProducer("group");
        //2.设定发送的命名服务器地址
        producer.setNamesrvAddr("localhost:9876");
        //3.启动发送的服务
        producer.start();
        for (int i = 1; i < 10; i++) {
            //4.创建要发送的消息对象,指定topic,指定内容body
            Message msg = new Message("topic",(i+"hello rocketmq").getBytes("UTF-8"));
            //发送消息
            SendResult result = producer.send(msg);
            System.out.println("返回结果: " + result);
        }


        //关闭连接
        producer.shutdown();

    }
}
